// 238.除自己以外数组的乘积
package LeetcodeHot100;

public class Solution238 {
    public int[] productExceptSelf(int[] nums) {
        int[] leftProd = new int[nums.length];
        int[] rightProd = new int[nums.length];
        leftProd[0] = nums[0];
        rightProd[nums.length - 1] = nums[nums.length - 1];
        for (int i = 1; i < nums.length; i++) {
            leftProd[i] = leftProd[i - 1] * nums[i];
        }
        for (int i = nums.length - 2; i >= 0; i--) {
            rightProd[i] = rightProd[i + 1] * nums[i];
        }
        int[] result = new int[nums.length];
        result[0] = rightProd[1];
        result[nums.length - 1] = leftProd[nums.length - 2];
        for (int i = 1; i < nums.length - 1; i++) {
            result[i] = leftProd[i - 1] * rightProd[i + 1];
        }
        return result;
    }
}
